#include "stdafx.h"
#include "UtilInstance.h"
#include "trex.h"

CUtilInstance::CUtilInstance(void)
{
}

CUtilInstance::~CUtilInstance(void)
{
}


char* CUtilInstance::WCharToChar(WCHAR* wchars)
{
	if (wchars == NULL)
		return NULL;

	char* pStr = NULL; 
#ifndef LINUX_NAS
	int strSize = WideCharToMultiByte(CP_ACP, 0, wchars, -1, NULL, 0, NULL, NULL);
	pStr = new char[strSize + 1];
	WideCharToMultiByte(CP_ACP, 0, wchars, -1, pStr, strSize, 0,0);
#else
	int strsize = wcstombs(NULL, wchars, 0);
	pStr = new char[strsize +  1];
	memset(pStr, 0, strsize + 1);
	wcstombs(pStr, wchars, strsize);
#endif
	return pStr;

}

WCHAR* CUtilInstance::CharToWChar(char* chars)
{
	if (chars == NULL)
		return NULL;

	WCHAR* wchars = NULL;
#ifndef LINUX_NAS
	int strSize = MultiByteToWideChar(CP_ACP, 0, chars, -1, NULL, 0);
	wchars = new WCHAR[strSize + 1];
	MultiByteToWideChar(CP_ACP, 0, chars, -1, wchars, strSize);
#else
	int strsize = mbstowcs(NULL, chars, 0);
	wchars = new WCHAR[strsize + 1];
	memset(wchars, 0, (strsize + 1) * sizeof(WCHAR));
	mbstowcs(wchars, chars, strsize);
#endif
	return wchars;
}

#ifndef LINUX_NAS
CTime CUtilInstance::MsToCTime(__int64 ms)
{
	CTime ctime;
	ctime = ms;
	return ctime;
}

__int64 CUtilInstance::CTimeToMs(CTime ctime)
{
	return ctime.GetTime();
}

__int64 CUtilInstance::GetCurrentTimeByMs()
{
	CTime ctime = CTime::GetCurrentTime();
	return CTimeToMs(ctime);
}

CTime CUtilInstance::GetCurrentTimeByCTime()
{
	return CTime::GetCurrentTime();
}

#endif

void sprintf_16u(char* dst, unsigned int value, unsigned int length)
{
	memset(dst, '0', length);
	dst[length] = 0;

	while(value)
	{
		char hex = value % 16;

		if(hex < 10)
			hex += 0x30;
		else
			hex += 0x61 - 10;

		dst[length-1] = hex;
		length--;

		value /= 16;
	}
}

char* CUtilInstance::GetNewGuid()
{
	SYSTEMTIME timestamp;
	GetLocalTime(&timestamp);
	//CTime ctime;
	//ctime = timestamp;
	time_t ctime = time(NULL);

	char* guid = new char[39];
	memset(guid, 0, 39);	

	unsigned int id1;
	unsigned short id2, id3, id4;
	unsigned short id5_low;
	unsigned int id5_high;

	//id1 = (timestamp.wMilliseconds + ctime.GetTime() & 0xFFFFFFFF) & 0xFFFFFFFF;
	id1 = (timestamp.wMilliseconds + ctime & 0xFFFFFFFF) & 0xFFFFFFFF;
	id2 = timestamp.wMilliseconds * (timestamp.wMinute + 1);
	id3 = timestamp.wMilliseconds * (timestamp.wHour + 1);
	id4 = timestamp.wMilliseconds * (timestamp.wSecond + 1);
	id5_low = timestamp.wMilliseconds * (timestamp.wMonth + 1);
	//id5_high = (timestamp.wMilliseconds + (ctime.GetTime() >> 32) & 0xFFFFFFFF) & 0xFFFFFFFF;
	id5_high = (timestamp.wMilliseconds + (ctime >> 32) & 0xFFFFFFFF) & 0xFFFFFFFF;

	//sprintf(guid, "{%08x-%04x-%04x-%04x-%04x%08x}", id1, id2, id3, id4, id5_low, id5_high);
	char tempid[32];

	guid[0] = '{';
	sprintf_16u(tempid, id1, 8); memcpy(guid+1, tempid, 8);
	guid[9] = '-';
	sprintf_16u(tempid, id2, 4); memcpy(guid+10, tempid, 4);
	guid[14] = '-';
	sprintf_16u(tempid, id3, 4); memcpy(guid+15, tempid, 4);
	guid[19] = '-';
	sprintf_16u(tempid, id4, 4); memcpy(guid+20, tempid, 4);
	guid[24] = '-';
	sprintf_16u(tempid, id5_low, 4); memcpy(guid+25, tempid, 4);
	sprintf_16u(tempid, id5_high, 8); memcpy(guid+29, tempid, 8);
	guid[37] = '}';

	guid[1] = 'f';
	//--End

	return guid;
}

WCHAR* CUtilInstance::MakeMessageText(WCHAR* strMessage, WCHAR* strReplace)
{
	if(strMessage == NULL)
		return NULL;

	if(strReplace == NULL)
		return strMessage;

	WCHAR *strReturn;
	strReturn = new WCHAR[wcslen(strMessage) + wcslen(strReplace)];

	int resolvedSQLLength = 0;
	int pcount = 0;

	for(unsigned int i=0; i<wcslen(strMessage); i++)
	{
		if(strMessage[i] == '$')
		{
			wcscpy(strReturn+resolvedSQLLength, strReplace);
			resolvedSQLLength += wcslen(strReplace);
		}
		else
		{
			strReturn[resolvedSQLLength] = strMessage[i];
			resolvedSQLLength++;
		}
	}

	strReturn[resolvedSQLLength] = NULL;

	return strReturn;
}

void CUtilInstance::StringToSystemtime(char* str_time, SYSTEMTIME& stu_time)
{
	if (str_time == NULL || strlen(str_time) == 0)
		return;

	int int_year = 0, int_month = 0, int_day = 0, int_hour = 0, int_minute = 0, int_second = 0, int_milisecond = 0;
	if (sscanf(str_time, "%04d-%02d-%02d %02d:%02d:%02d", &int_year, &int_month, &int_day, &int_hour, &int_minute, &int_second) < 0)
	{
		return;
	}

	stu_time.wYear = int_year;
	stu_time.wMonth = int_month;
	stu_time.wDay = int_day;
	stu_time.wHour = int_hour;
	stu_time.wMinute = int_minute;
	stu_time.wSecond = int_second;
	stu_time.wMilliseconds = int_milisecond;
}

void CUtilInstance::StringToInt64(char* str_time, __int64& i64_time)
{
	if (str_time == NULL || strlen(str_time) == 0)
		return;

	int int_year = 0, int_month = 0, int_day = 0, int_hour = 0, int_minute = 0, int_second = 0, int_milisecond = 0;
	if (sscanf(str_time, "%04d-%02d-%02d %02d:%02d:%02d", &int_year, &int_month, &int_day, &int_hour, &int_minute, &int_second) < 0)
	{
		return;
	}

	FILETIME stu_filetime;
	SYSTEMTIME stu_systemtime;

	stu_systemtime.wYear = int_year;
	stu_systemtime.wMonth = int_month;
	stu_systemtime.wDay = int_day;
	stu_systemtime.wHour = int_hour;
	stu_systemtime.wMinute = int_minute;
	stu_systemtime.wSecond = int_second;
	stu_systemtime.wMilliseconds = int_milisecond;

	SystemTimeToFileTime(&stu_systemtime, &stu_filetime);

	i64_time = *((__int64*)&stu_filetime);
}

void CUtilInstance::SystemtimeToString(SYSTEMTIME stu_systemtime, char* str_time)
{
	if (str_time == NULL)
		return;

	sprintf(str_time, "%04d-%02d-%02d %02d:%02d:%02d",
		stu_systemtime.wYear, stu_systemtime.wMonth, stu_systemtime.wDay,
		stu_systemtime.wHour, stu_systemtime.wMinute, stu_systemtime.wSecond);
}

void CUtilInstance::FiletimeToString(FILETIME stu_filetime, char* str_time)
{
	if (str_time == NULL)
		return;
			
	SYSTEMTIME stu_systemtime;
	FileTimeToSystemTime(&stu_filetime, &stu_systemtime);

	sprintf(str_time, "%04d-%02d-%02d %02d:%02d:%02d",
		stu_systemtime.wYear, stu_systemtime.wMonth, stu_systemtime.wDay,
		stu_systemtime.wHour, stu_systemtime.wMinute, stu_systemtime.wSecond);
}

void CUtilInstance::Int64ToTimeString(__int64 i64_time, char* str_time)
{
	if (str_time == NULL)
		return;

	FILETIME stu_filetime;
	*((__int64*)(&stu_filetime)) = i64_time;
	
	FiletimeToString(stu_filetime, str_time);
}

void CUtilInstance::Int64ToTimeStringNoMark(__int64 i64_time, char* str_time)
{
	if (str_time == NULL)
		return;

	FILETIME stu_filetime;
	*((__int64*)(&stu_filetime)) = i64_time;

	SYSTEMTIME stu_systemtime;
	FileTimeToSystemTime(&stu_filetime, &stu_systemtime);

	sprintf(str_time, "%04d%02d%02d%02d%02d%02d",
		stu_systemtime.wYear, stu_systemtime.wMonth, stu_systemtime.wDay,
		stu_systemtime.wHour, stu_systemtime.wMinute, stu_systemtime.wSecond);

}

void CUtilInstance::ResolutionToWidthHeight(char* str_resolution, int& int_width, int& int_height)
{
	if (str_resolution == NULL || strlen(str_resolution) == 0)
		return;

	char str_temp[128] = {0};
	strcpy(str_temp, str_resolution);
	char* ptr_position = NULL;
	if ((ptr_position = strstr(str_temp, "x")) != NULL)
		*ptr_position = '\0';

	int_width = strtol(str_temp, NULL, 10);
	int_height = strtol(ptr_position + 1, NULL, 10);
}

__int64 CUtilInstance::GetLocalTimeToInt64()
{
	SYSTEMTIME stu_cur_systemtime;
	FILETIME stu_cur_filetime;
	__int64 i64_cur_time = 0;
	GetLocalTime(&stu_cur_systemtime);
	SystemTimeToFileTime(&stu_cur_systemtime, &stu_cur_filetime);
	i64_cur_time = *(__int64*)&stu_cur_filetime;

	return i64_cur_time;
}

void CUtilInstance::GetLocalTimeToString(char* str_time)
{
	SYSTEMTIME stu_systemtime;
	GetLocalTime(&stu_systemtime);
	sprintf(str_time, "%04d-%02d-%02d %02d:%02d:%02d",
		stu_systemtime.wYear, stu_systemtime.wMonth, stu_systemtime.wDay,
		stu_systemtime.wHour, stu_systemtime.wMinute, stu_systemtime.wSecond);
}